//
//  ZZComposeBottomView.swift
//  web
//
//  Created by 张卓 on 2017/2/9.
//  Copyright © 2017年 ZZ. All rights reserved.
//

import UIKit


//定义枚举
enum ZZComposeButtonViewType : Int {
    //图片
    case picture = 0
    //@
    case mention = 1
    // #
    case trend = 2
    //笑脸
    case emoticon = 3
    // +
    case add = 4
    
}

class ZZComposeBottomView: UIView {
    //1.声明闭包
    var closure:((ZZComposeButtonViewType)->())?
    
    //表情按钮
    var emoticonBnt: UIButton?
    //提供一个属性 判断是表情键盘还是系统键盘
    var isEmoticon: Bool = false{
        didSet{
            
            var image = ""
            //如果是自定义表情键盘
            if isEmoticon {
                //图片改成的键盘图片
                image = "compose_keyboardbutton_background"
              
            }else{
            //如果是系统键盘
                // 显示笑脸图片
                image = "compose_emoticonbutton_background"
            }
            emoticonBnt?.setImage(UIImage(named:image), for: UIControlState.normal)
            emoticonBnt?.setImage(UIImage(named:"\(image)_highlighted"), for: UIControlState.highlighted)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //监听事件
    @objc private func buttonClick(button: UIButton){
        //3. 执行
        /// 通过枚举值转换成枚举名
        closure?(ZZComposeButtonViewType(rawValue: button.tag)!)
    }
    
    //MARK: - 设置视图
    private func setupUI(){
        let pictureBnt = addChildButtons(imgName: "compose_toolbar_picture", type: .picture)
        let mentionBnt = addChildButtons(imgName: "compose_mentionbutton_background", type:  .mention)
        let trendBnt = addChildButtons(imgName: "compose_trendbutton_background",type: .trend)
        emoticonBnt = addChildButtons(imgName: "compose_emoticonbutton_background",type: .emoticon)
        let addBnt = addChildButtons(imgName: "compose_add_background", type: .add)
        
        //设置约束
        pictureBnt.snp_makeConstraints { (make) in
            make.left.top.bottom.equalTo(self)
            make.width.equalTo(mentionBnt)
        }
        mentionBnt.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(pictureBnt.snp_right)
            make.width.equalTo(trendBnt)
        }
        trendBnt.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(mentionBnt.snp_right)
            make.width.equalTo(emoticonBnt!)
        }
        emoticonBnt!.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(trendBnt.snp_right)
            make.width.equalTo(addBnt)
        }
        addBnt.snp_makeConstraints { (make) in
            make.top.bottom.right.equalTo(self)
            make.left.equalTo(emoticonBnt!.snp_right)
        }
    }
    
    
    //MARK: - 定义一个方法 ,负责创建button
    private func addChildButtons(imgName:String, type: ZZComposeButtonViewType)-> UIButton{
        //实例化按钮
        let button = UIButton()
        //设置tag
        button.tag = type.rawValue
        //添加事件
        button.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
        //设置图片
        button.setImage(UIImage(named:imgName), for: UIControlState.normal)
        button.setImage(UIImage(named:"\(imgName)_highlighted"), for: UIControlState.highlighted)
        //设置背景图片
        button.setBackgroundImage(UIImage(named:"compose_toolbar_background"), for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named:"compose_toolbar_background"), for: UIControlState.highlighted)
        addSubview(button)
        return button
    }
    
}
